<?php
class HotelController extends Apps_Controller_Action_Default {
	public function indexAction() {
	    $countryModel = new M_Country();
	    $areaModel    = new M_Area();
	    $regionModel  = new M_Region();
	    
// 	    $country = $countryModel->listCountry();
// 	    if($country) {
// 	        $country = $country->query()->fetchAll();
// 	        $regionModel = new M_Region();
// 	        foreach($country as $k => $v) {
//                 $country[$k]['list_region'] = $regionModel->listRegionByCountryId($v['id'])->query()->fetchAll();
// 	        }
// 	        $this->view->country = $country;
// 	    }

// 	    //Hotel and resort: by location, coutry
//         $listCountry = $countryModel->listCountry()->query()->fetchAll();
//         if($listCountry) {
//             foreach($listCountry as $k => $v) {
//                 $listCountry[$k]['list_region'] = $regionModel->listRegionByCountryId($v['id'])->query()->fetchAll();
//             }
//         }
        
        //Hotel and resort: by location, coutry
        $listCountry = $countryModel->listCountry()->query()->fetchAll();
        if($listCountry) {
        	foreach($listCountry as $k => $v) {
        		if($v['id'] == 1) { //Check is VIETNAM
        			$area = $areaModel->listareaByCountryId($v['id'])->query()->fetchAll();
        			if($area) {
        				foreach($area as $key => $value) {
        					$region = $regionModel->listRegionHotelByAreaId($value['id'])->query()->fetchAll();
        					$area[$key]['list_region'] = $region;
        				}
        				$listCountry[$k]['area'] = $area;
        			}
        		} else {
        			$listCountry[$k]['list_region'] = $regionModel->listRegionByCountryId($v['id'])->query()->fetchAll();
        			$listCountry[$k]['area'] = NULL;
        		}
        	}
        }
        $this->view->listCountry = $listCountry;
	}

	public function regionAction() {
        $rq = $this->_request;
        $regionId = intval($rq->getParam('id'));

        $validator = new Zend_Validate_Db_NoRecordExists(array('table' => 'region', 'field' => 'id'));
        if(!$regionId || $validator->isValid($regionId)) {
            $this->_helper->redirector('index');
        } else {
    	    $countryModel = new M_Country();
    	    $areaModel    = new M_Area();
            $regionModel  = new M_Region();
            $hotelModel   = new M_Hotel();
            $tbHotelType  = new M_HotelType();

            $listHotelType = $tbHotelType->listHotelType()->query()->fetchAll();

            foreach($listHotelType as $k => $v) {
                $listHotelType[$k]['list_hotel'] = $hotelModel->listHotelByRegion($regionId, $v['id'])->query()->fetchAll();
            }
            $this->view->hotel = $listHotelType;

            $region = $regionModel->getRegionById($regionId)->query()->fetch();
            $this->view->region = $region;

            //Hotel and resort: by location, coutry
            $listCountry = $countryModel->listCountry()->query()->fetchAll();
            $listCountryNew = array();
	        if($listCountry) {
	        	foreach($listCountry as $k => $v) {
	        		if($v['id'] == $region['country_id']) {
		        		if($v['id'] == 1) { //Check is VIETNAM
		        			$area = $areaModel->listareaByCountryId($v['id'])->query()->fetchAll();
		        			if($area) {
		        				foreach($area as $key => $value) {
		        					$region = $regionModel->listRegionHotelByAreaId($value['id'])->query()->fetchAll();
		        					$area[$key]['list_region'] = $region;
		        				}
		        				$listCountry[$k]['area'] = $area;
		        			}
		        		} else {
		        			$listCountry[$k]['list_region'] = $regionModel->listRegionByCountryId($v['id'])->query()->fetchAll();
		        			$listCountry[$k]['area'] = NULL;
		        		}
		        		$listCountryNew[$k] = $listCountry[$k];
	        		}	
	        	}
	        }
	        $this->view->listCountry = $listCountryNew;
        }
	}

	public function detailAction() {
        $rq = $this->_request;
        $id = intval($rq->getParam('id'));

        $validator = new Zend_Validate_Db_NoRecordExists(array('table' => 'hotel', 'field' => 'id'));
        if(!$id || $validator->isValid($id)) {
            $this->goBack();
        } else {
            $hotelModel = new M_Hotel();
            $hotel = $hotelModel->getHotelById($id)->query()->fetch();

            $tbHotelPrice = new M_DbTable_HotelPrice();
            $hotelPrice = $tbHotelPrice->fetchAll(array('hotel_id =?' => $id))->toArray();


            $photoModel = new M_PhotoModel();
            $photo = $photoModel->listPhotoByReference($id, M_DbTable_Photo::REFERENCE_TYPE_HOTEL);

            $tbVctInfo  = new M_DbTable_VctInfo();
			$vctInfo = $tbVctInfo->fetchRow();
			if($vctInfo) {
			    $vctInfo = $vctInfo->toArray();
                $this->view->vctInfo 	= $vctInfo;
			}

			//meta tag
			$tbVctSetting = new M_DbTable_VctSetting();
			$vctSetting   = $tbVctSetting->getMetaTag();
			$this->_page_meta_keywords    = $hotel['meta_keyword'] ? $hotel['meta_keyword'] : ($vctSetting['meta_keyword'] ? $vctSetting['meta_keyword'] : $this->_default_keyword);
			$this->_page_meta_description = $hotel['meta_description'] ? $hotel['meta_description'] : ($vctSetting['meta_description'] ? $vctSetting['meta_description'] : $this->_default_description);
			$this->_page_title 	  		  = $hotel['header_title'] ? $hotel['header_title'] : ($vctSetting['header_title'] ? $vctSetting['header_title'] : $this->_default_header_title);

            $this->view->hotelPrice = $hotelPrice;
            $this->view->hotel 		= $hotel;
            $this->view->photo 		= $photo->query()->fetchAll();
        }
	}
}